/**
 * jQuery Round Borders
 *
 * Crea un effetto bordo arrotondado su alcuni o tutti gli angoli di una immagine
 * tramite sovrapposizione.
 *
 * @author			Giovambattista Fazioli
 * @email			g.fazioli@undolog.com
 * @web				http://www.undolog.com
 *
 * @usage
 *	$('myimageid').roundBorders(
 *						{
 *							image	: 'path/myborder.png',
 *							radius	: 16,
 *							corners	: 'tl br'
 *						}
 *	);
 *
 *
 */

(function($) {
	jQuery.fn.roundBorders = function(settings){
		return this.each( 
			function() {
				var $this 		= $(this);
				var hash		= 'ba4aae84ef81'; // paganini not repeat
				
				if( !$this.hasClass( hash ) ) {
				
					$this.css('display','block');
					
					var defaults = {
						radius:			settings.radius		|| 16,
						image:			settings.image		|| 'border.png',
						width: 			settings.width  	|| $this.attr('width'),
						height: 		settings.height 	|| $this.attr('height'),
						center:			settings.center 	|| 'no',
						corners:		settings.corners 	|| 'tl tr bl br'
					};
					
					var ra	= defaults.radius;
					var ww	= defaults.width;
					var hh	= defaults.height;
					var im	= defaults.image;
					var to  = -hh;
					var tr 	= ww-ra;
					var bl 	= to+hh-ra;
					var cc	= (defaults.center == 'no')?'':'margin:0 auto';
					
					var out = '';
					var cr	= defaults.corners.split(' ');
					var co	= {};
					
					co.tl	= '<div style="background:url('+im+') no-repeat;position:absolute;width:'+ra+'px;height:'+ra+'px;margin:'+to+'px 0 0 0"></div>';
					co.tr	= '<div style="background:url('+im+') -'+ra+'px 0px no-repeat;position:absolute;width:'+ra+'px;height:'+ra+'px;margin:'+to+'px 0 0 '+tr+'px"></div>';
					co.bl	= '<div style="background:url('+im+') -'+(ra*2)+'px 0px no-repeat;position:absolute;width:'+ra+'px;height:'+ra+'px;margin:'+bl+'px 0 0 0"></div>';
					co.br	= '<div style="background:url('+im+') -'+(ra*3)+'px 0px no-repeat;position:absolute;width:'+ra+'px;height:'+ra+'px;margin:'+bl+'px 0 0 '+tr+'px"></div>';
					
					for(var o in cr) if(co[cr[o]]) out += co[cr[o]];
					
					$this
						.wrap('<div style="width:'+ww+'px;height:'+hh+'px;'+cc+'"></div>')
						.after( out ).addClass( hash ).fadeIn('slow');
				}
			}
		);
	}
})(jQuery);